Page({

  /**
   * 页面的初始数据
   */
  data: {
    arr:{},
    show:false,
    buy:false,
    settlement:[],
    Array:{}, 
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.request({
      url: "https://bag.idianwei.com/index.php/Api/Api/cartList",
      data: {
        userId: 16,
        token: 1501221705,
      },
      success: (res) => {
        console.log(res);
        this.setData({
          arr: res.data.resultObj,
        });
        for(var i=0;i<this.data.arr.length;i++){
          this.data.arr[i].check=false;
        }
        console.log(this.data.arr);
        // console.log(res.data.resultObj.cartId);
        if (this.data.arr.length == undefined) {
          this.setData({
            buy: false,
          })
        }else{
          this.setData({
            buy: true,
          })
        }
      }
    })
  },
  // 加
  add:function(own){
    wx.request({
      url: "https://bag.idianwei.com/index.php/Api/Api/addCart",
      data:{
        userId:22,
        token: 1516268418139,
        goodsId: own.currentTarget.dataset.id,
      },
      success:(res)=>{
        console.log(res);
        // for(var i=0;i<this.data.arr.length;i++){
        //   if (this.data.arr[i].goodsId == own.currentTarget.dataset.id){
        //     this.data.arr[i].goodsCnt+=1;
        //   }
        // }
      }
    })
  },
  // 减
  Reduction:function(own){
    wx.request({
      url: "https://bag.idianwei.com/index.php/Api/Api/editCart",
      data: {
        userId: 22,
        token: 1516268418139,
        cartId: own.currentTarget.dataset.id,
      },
      success: (res) => {
        console.log(res);
        // this.data.arr.goodsCnt -= this.data.arr.goodsCnt;
        // if (this.data.arr.goodsCnt == 1){
        //   this.data.arr.goodsCnt = 1;
        // }
      }
    })
  },
  box:function(own){
    for(var i=0;i<this.data.arr.length;i++){
      if (this.data.arr[i].goodsId == own.currentTarget.dataset.id){
        this.data.arr[i].check = false;
      }
      
    }
    if (this.data.arr[own.currentTarget.dataset.id].check == true) {
      this.data.arr[own.currentTarget.dataset.id].check = false;
    } else {
      this.data.arr[own.currentTarget.dataset.id].check = true;
    }
    console.log(this.data.arr);
    
    
    // for (var i = 0; i < this.data.settlement.length;i++){
    //   if (this.data.settlement[i].check==false){
    //     this.data.settlement[i].splice(i,1)
    //   }
    // }
    
  },
  // 删除
  remove:function(own){
    
    wx.request({
      url: "https://bag.idianwei.com/index.php/Api/Api/delCart",
      data: {
        userId: 22,
        token: 1516268418139,
        cartId: own.currentTarget.dataset.id,
      },
      success: (res) => {
        console.log(res);
      }
    })
  },
  // 结算
  settlement: function (own) {
    for (var i = 0; i < this.data.arr.length; i++) {
      console.log(this.data.arr);
      if (this.data.arr[i].check == true) {
        this.data.settlement.push(this.data.arr[i]);
      } 
    }
    console.log(this.data.settlement);



    var number_id=0;
    for (var i = 0; i < this.data.settlement.length;i++){
      console.log(this.data.settlement[i]);
      number_id += this.data.settlement[i].cartId.length;
    }
    console.log(this.data.settlement);
    if(number_id.length==0){
      wx.showModal({
        title: '提示',
        content: '女王大人，您未选中商品！',
      })
      return;
    }else{
      this.data.Array.data = this.data.settlement;
      var oStr = JSON.stringify(this.data.Array);
      wx.navigateTo({
        url: '/pages/index/Make/Make?id=' + oStr,
      })
    }
    


    
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },
  

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  },
  editor:function(){
    if(this.data.show==false){
      this.setData({
        show:true,
      })
    }else{
      this.setData({
        show:false,
      })
    }
  }

})